<?php 
if (!defined('BASEPATH')) exit('No direct access allowed.'); 

class MY_Model extends CI_Model { 
	public $err_arr;
	public $table;
	public $tbl_key = '';
	public $tbl_ext = '';
	
	function __construct()
    {
        parent::__construct();
    }
	
	function addError($err){
		$this->err_arr[] = $err; 
	}
	
	function getError($index=-1){
		if($index==-1){
			return $this->err_arr;
		}
		return isset($this->err_arr[$index])?$this->err_arr[$index]:'';
	}
	
	function getTable(){
		return $this->table;
	}
	/**
	* 获取单条信息
	*
	* @param string $masterid ID
	*/

	public function getRow($searchdata, $selectField = '*', $sort_field = '', $asc = 'desc')
	{

		$this->db->select($selectField);
		$this->db->from($this->table);

		$this->db->where($searchdata);
				
		if(!empty($sort_field)){
			$this->db->order_by($sort_field, $asc);
		}
		
		$this->db->limit(1);
        $query = $this->db->get();
		return $query->row_array(); 
	}
	
	/**
	 * 列表
	 * @param int $offset 偏移量
	 * @param int $limit 显示条数
	 */

	public function getAll($searchdata=array(), $selectField = "*",  $limit = 15, $offset = 0, $sort_field = '', $asc = 'asc', $relation = '')
	{

		$this->db->select($selectField);
		$this->db->where($searchdata);
		
		if(!empty($sort_field)){
			$this->db->order_by($sort_field, $asc);
		}
		
		if ($limit == 0)
			$query = $this->db->get($this->table);
		else
			$query = $this->db->get($this->table, $limit, $offset);
		return  $query->result_array();
	}
	
	
	/**
	* 统计总数
	* @param string $searchdata 要检索的条件数组字符串
	*/

	public function getCount($searchdata=array()){
		$this->db->from($this->table);
		$this->db->where($searchdata);
		return $this->db->count_all_results();
	}
	
	/**
	* 添加
	* @param string 
	*/

	public function add($data){
		$this->db->insert($this->table,$data);
		return $this->db->insert_id();
	}
	
	/**
	* 编辑数据
	* @param string
	*/

	public function edit($data,$where){
		
		return $this->db->update($this->table,$data,$where);
	}
	/**
	* 删除数据
	* @param string 
	*/

	public function del($where){
		
		return $this->db->delete($this->table,$where);
	}
	/**
	* 获取数据
	* @param string 
	*/
	function id_get_info($id){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where id='{$id}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	
	/**
	* 获取数据
	* @param string 
	*/
	function _id_get_info($id){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where id='{$id}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	
	function _field_get_info($id,$field='id',$select='*'){
		$sql  = "select {$select} from ".$this->db->dbprefix.$this->table." where `{$field}`='{$id}' limit 1";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	
	//分页列表
	function _where_get_list($where="1",$page=1,$size=10,$orderby="order by id desc",$select="*",$as_name='',$join=''){
		
		if(empty($where)){
			$where = "1";
		}
		
		if(!empty($as_name)){
			$as_name = " as {$as_name}";
		}
		
		$sql  = "select {$select} from ".$this->db->dbprefix.$this->table." {$as_name} {$join} where {$where} {$orderby} LIMIT ".($page-1)*$size .",{$size}";
		$query = $this->db->query($sql);
		$res = $query->result_array();		
		
		$sql  = "select count(*) as num from ".$this->db->dbprefix.$this->table." {$as_name} {$join} where {$where} ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return array('list'=>$res,'count' =>$row['num']);
	}
	
	function _where_exist($arr=array()){
		
		
		if(is_array($arr)){
			$where = " 1 ";
			foreach($arr as $key=>$val){
				if(!is_array($val)){
					$where .= " AND `{$key}`='{$val}' ";
				}
			}
		}
		else{
			$where = $arr;
			
		}
		
		$sql  = "select id from ".$this->db->dbprefix.$this->table." where {$where} limit 1 ";
		
		
		$query = $this->db->query($sql);
		$arr = $query->result_array();
		if(!$arr || count($arr)<=0){
			return false;
		}
		
		return true;
	}
	//兼容旧的
	function _where_exits($arr=array()){
		return $this->_where_exist($arr);
	}
}